
package edu.whu.powersoap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.mapapi.cloud.CloudListener;
import com.baidu.mapapi.cloud.CloudManager;
import com.baidu.mapapi.cloud.CloudPoiInfo;
import com.baidu.mapapi.cloud.CloudSearchResult;
import com.baidu.mapapi.cloud.DetailSearchResult;
import com.baidu.mapapi.cloud.LocalSearchInfo;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

import edu.whu.powersoap.R;

import java.util.List;

public class ListDataActivity extends Activity implements CloudListener {
    private static final String TAG = "ListDataActivity";

    private Context mContext;
    private ProgressDialog mWaitDialog = null;
    private ListView mListView;
    private MapView mMapView;

    private BaiduMap mBaiduMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_data);

        mListView = (ListView) findViewById(R.id.dataListView);
//        mMapView = (MapView) findViewById(R.id.dataMapView);
//        mBaiduMap = mMapView.getMap();

        mContext = getBaseContext();

        if (mWaitDialog == null)
        {
            mWaitDialog = new ProgressDialog(this);
            mWaitDialog.setMessage("加载中...");
        }

        mWaitDialog.show();

        CloudManager.getInstance().init(ListDataActivity.this);

        LocalSearchInfo info = new LocalSearchInfo();
        info.ak = "A4916bd1a0ce990660677a7d773b7153";
        info.geoTableId = 69300;
        info.tags = "";
        info.q = "";
        info.region = "武汉市";
        info.pageSize = 50;
        CloudManager.getInstance().localSearch(info);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        CloudManager.getInstance().destroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onGetDetailSearchResult(DetailSearchResult result, int error) {
        // mWaitDialog.dismiss();

    }

    @Override
    public void onGetSearchResult(CloudSearchResult result, int error) {
        mWaitDialog.dismiss();
        Log.d(TAG, "onGetSearchResult error=" + error);
        if (result != null && result.poiList != null
                && result.poiList.size() > 0) {
            Log.i(TAG, "onGetSearchResult, result length: " + result.poiList.size());

            for (CloudPoiInfo info : result.poiList) {
                Log.d(TAG, "info=" + info);

            }

        }

        mListView.setAdapter(new PoiListAdapter(this, result.poiList));
        // setListViewHeightBasedOnChildren(mListView);

        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                CloudPoiInfo info = (CloudPoiInfo) v.getTag();

                BitmapDescriptor bd = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
                LatLng ll = new LatLng(info.latitude, info.longitude);
                OverlayOptions oo = new MarkerOptions().icon(bd).position(ll);
                mBaiduMap.clear();
                mBaiduMap.addOverlay(oo);
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 17);
                mBaiduMap.animateMapStatus(u);
            }

        });

    }

    private class PoiListAdapter extends BaseAdapter {

        private List<CloudPoiInfo> mList;
        private LayoutInflater mInflater;

        public PoiListAdapter(Context context, List<CloudPoiInfo> mList) {
            super();
            this.mList = mList;
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            if (convertView == null) {
                v = mInflater.inflate(android.R.layout.simple_list_item_2, parent, false);
            } else {
                v = convertView;
            }

            CloudPoiInfo info = (CloudPoiInfo) getItem(position);
            TextView text1 = (TextView) v.findViewById(android.R.id.text1);
            TextView text2 = (TextView) v.findViewById(android.R.id.text2);
            text1.setText(info.title);
            text2.setText(info.address);

            v.setTag(info);

            return v;
        }

    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
